#include <WiFi.h>
#include <HTTPClient.h>
const int BUTTON_PIN = 0;  // GPIO0
volatile bool buttonPressed = false;
//发件人的相关SMTP邮箱配置，需替换为实际值
String smtpEmail ="weihua-lau@qq.com";
const char* smtpCode="txsbindznvombaeb";
const char* smtpCodeType="qq";
void buttonISR() {
  buttonPressed = true;
}
void setup() {
  Serial.begin(115200);

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISR, FALLING);

  // 检查是否有保存的Wi-Fi配置信息
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.begin();  // 尝试自动连接上次保存的Wi-Fi
    Serial.println("尝试连接已保存的WiFi...");
    
    // 等待连接成功
    int attempts = 0;
    while (WiFi.status() != WL_CONNECTED && attempts < 20) {
      delay(500);
      Serial.print(".");
      attempts++;
    }
  }

  // 如果无法自动连接Wi-Fi，则进入SmartConfig模式
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("\n开始配网...");
    WiFi.beginSmartConfig();  // 启动SmartConfig

    while (!WiFi.smartConfigDone()) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("配网成功");
  }

  // 确保连接成功
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi已连接.");
  Serial.print("IP地址: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // 在此处可以添加其他代码
  if (buttonPressed) {
    buttonPressed = false;
    Serial.println("按下按键,重新配网");

    WiFi.disconnect();
    WiFi.mode(WIFI_STA);

    // 启动SmartConfig
    WiFi.beginSmartConfig();
    Serial.println("等待SmartConfig完成...");
    while (!WiFi.smartConfigDone()) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("SmartConfig完成");

    // 等待新的Wi-Fi连接成功
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("\nWiFi已连接.");
    Serial.print("IP地址: ");
    Serial.println(WiFi.localIP());
    String mailContent = "IP地址:" + WiFi.localIP().toString();
    	//接收邮箱、标题、主题、内容
	  sendEmailMessage("1265477411@qq.com","esp32","smartconfig",mailContent);

  }
}
//发送邮件的函数，收件人，标腿，主题，内容
void sendEmailMessage(String recipient, String fromTitle, String subject,String messageContent)
{
  if(WiFi.status() != WL_CONNECTED)
  {
    //首先检查WiFi是否已经连接、如果没
    Serial.println("WiFi not connected");
    return;
  }
  HTTPClient http;
  http.setTimeout(60000);
  //构建POST请求的数据，拼接各项参数,创建一个HTTPClient对象，用于后续发送
  String postPayload = "ColaKey=XUjI6EOsdYn23n1745306215245QhShPo5cgK&tomail=" + recipient + "&fromTitle=" + fromTitle + "&subject=" + subject + "&content=" + messageContent + "&smtpCode=" + smtpCode + "&smtpEmail=" + smtpEmail + "&smtpCodeType=" + smtpCodeType + "&isTextContent=true";
  // 输出postpayload
  Serial.println(postPayload);
  //开始连接指定的邮件发送服务URL??
  http.begin("https://luckycola.com.cn/tools/customMail");
  //设置请求头，指定内容类型为表单数据格式,为了让邮件发送服务能正确解析接收
  http.addHeader("Content-Type","application/x-www-form-urlencoded");
  //发送POST请求
  int responseCode = http.POST(postPayload);
  if (responseCode > 0) {
    Serial.print("邮件发送成功, 响应码: ");
    Serial.println(responseCode);
  } else {
    Serial.print("邮件发送失败, 响应码: ");
    Serial.println(responseCode);
    Serial.print("错误信息: ");
    Serial.println(http.errorToString(responseCode));
  }
  http.end();

}